package com.sld.integrate.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceRequest;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceResponse;

/**
 * 描述:
 *
 * @Author tanli
 * @create 2022-05-13 14:11
 */
public class ServiceInvokeUtil {

    /**
     * @param Identifier：服务的Identifier，必须传入。
     * @param Args：要启用服务的入参信息，必须传入。
     */
    public static InvokeThingServiceResponse.Data InvokeThingService(DefaultAcsClient client,
                                                                     String InstanceId,
                                                                     String IotId,
                                                                     String ProductKey,
                                                                     String DeviceName,
                                                                     String Identifier,
                                                                     String Args) {
        InvokeThingServiceResponse response =null;
        InvokeThingServiceRequest request = new InvokeThingServiceRequest();
        request.setArgs(Args);
        request.setDeviceName(DeviceName);
//        request.setIotId(IotId);
        request.setIdentifier(Identifier);
        request.setProductKey(ProductKey);
        request.setIotInstanceId(InstanceId);

        try {
            response = client.getAcsResponse(request);

            if (response.getSuccess() != null && response.getSuccess()) {
                System.out.println("服务执行成功");
            } else {
                System.out.println("服务执行失败");
            }
            System.out.println(JSON.toJSONString(response));
            return response.getData();

        } catch (ClientException e) {
            e.printStackTrace();
            System.out.println("服务执行失败！" + JSON.toJSONString(response));
        }
        return null;
    }
}
